package com.bytecodezz.photoshop.compressor

import android.graphics.Bitmap
import android.text.TextUtils
import android.util.Log

/**
 * 图片压缩处理的工具类
 * 向上提供底层处理能力
 * 提供API:
 * compressBitmap(Bitmap,Int,String,Boolean,OnCompressorListener)
 * Bitmap:需要压缩的图片
 * Int:压缩阀值
 * String:压缩后图片输出路径
 * Boolean:是否开启线程进行压缩，默认开启
 * OnCompressorListener:压缩完成监听，默认为null
 *
 */
class CompressorTools {
    var DEBUG: Boolean = true

    init {
        System.loadLibrary("compressorJpeg")
    }

    private var onCompressorListener: OnCompressorListener? = null

    fun compressBitmap(
        bitmap: Bitmap,
        quality: Int,
        outputPath: String,
        openChildThread: Boolean = true,
        listener: OnCompressorListener? = null
    ) {
        checkParams(bitmap, quality, outputPath)
        if (bitmap.isRecycled) {
            listener?.let {
                if (DEBUG) Log.d("compressor-java", "bitmap已经被回收，将直接回调null")
                it.onCompressorResult(null, null)
            }
            return
        }
        if (TextUtils.isEmpty(outputPath)) {

        }
        onCompressorListener = listener
        if (DEBUG) Log.d("compressor-java", "开始传入Native层进行处理")
        nativeCompressor(bitmap, quality, outputPath, openChildThread)
    }

    private fun checkParams(
        bitmap: Bitmap,
        quality: Int,
        outPutPath: String
    ) {

    }

    /**
     * 为jni提供调用
     * 当视频准备好之后会回调该方法
     */
    private fun onCompressorForNative(rawBitmap: Bitmap?, outputPath: String?) {
        onCompressorListener?.let {
            it.onCompressorResult(rawBitmap, outputPath)
        }
    }

    /**
     * 为jni提供调用
     * 当视频准备好之后会回调该方法
     */
    private fun testForNative() {
        onCompressorListener?.let {
            it.onCompressorResult(null, null)
        }
    }

    private external fun versionNative()

    private external fun nativeCompressor(
        bitmap: Bitmap,
        quality: Int,
        outPutPath: String,
        openChildThread: Boolean
    )
}